/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.ourgame.tankgame.test.ld;

import com.jme3.math.Vector3f;
import com.jme3.scene.Node;
import com.jme3.scene.Spatial;

/**
 *
 * @author L
 */
public class CaterpillarSys {

    /**
     * 履带节数
     */
    private int caterpillarCount;
    /**
     * 运行路径
     */
    private Vector3f[] pathVecs;
    /**
     * 履带模型
     */
    private Spatial caterpillarModel;
//    private Node rootNode;

    public CaterpillarSys(int caterpillarCount, Vector3f[] pathVecs, Spatial caterpillarModel) {
        this.caterpillarCount = caterpillarCount;
        this.pathVecs = pathVecs;
        this.caterpillarModel = caterpillarModel;
//        this.rootNode = rootNode;
//        initCaterpillar();
    }

    public void initCaterpillar(Node rootNode) {
        if (caterpillarCount > pathVecs.length) {
            return;
        }
        for (int i = 0; i < caterpillarCount; i++) {
            Vector3f[] path = reSortPath(i);
            Spatial caterpillar = caterpillarModel.clone();
            rootNode.attachChild(caterpillar);
            MontionSys.generateMontion(caterpillar, path, null, null).play();
        }
    }

    private Vector3f[] reSortPath(int caterpillarIndex) {
        Vector3f[] path = new Vector3f[pathVecs.length];

        for (int i = 0; i < path.length; i++) {
            if (caterpillarIndex == pathVecs.length) {
                caterpillarIndex = 0;
            }
            path[i] = pathVecs[caterpillarIndex++];
        }
        return path;
    }
}
